angular.module('masvida.services', [])
.constant('MasVidaApiUrl', 'http://masvidafellowshipone.azurewebsites.net/api')
.service('LoginService', function ($http, MasVidaApiUrl) {
    return {
        loginUser: function (username, password, loginType) {
            var url = MasVidaApiUrl + '/Login/Login';

            var user = {
                Username: username,
                Password: password,
                LoginType: loginType,
                Ticket: null,
                ApiKey: null
            }

            return $http
                .post(url, angular.toJson(user));
        },
        isUserLogged: function () {
            return window.localStorage['user'] != undefined && window.localStorage['user'] != null && window.localStorage['user'] != "null";
        },
        logout: function () {
            window.localStorage['user'] = null;
        },
        saveUser: function (user) {
            window.localStorage['user'] = JSON.stringify(user);
        },
        getUserLogged: function () {
            return JSON.parse(window.localStorage['user'] || '{}');
        }
    }
})

.service('AttendanceService', function ($http, MasVidaApiUrl) {
    return {
        getGroups: function (request) {
            var url = MasVidaApiUrl + "/Group/GetGroupsForUser";
            return $http
                .post(url, angular.toJson(request));
        },
        getAttendances: function(request) {
            var url = MasVidaApiUrl + "/AttendanceSummary/GetAttendanceSummaryByGroup";
            return $http
                .post(url, angular.toJson(request));
        },
        getAttendanceDetailForAttendanceId: function(request) {
            var url = MasVidaApiUrl + "/Attendance/GetAttendancesByAttendanceSummaryId";
            return $http
                .post(url, angular.toJson(request));
        },
        saveAttendance: function(request) {
            var url = MasVidaApiUrl + "/AttendanceSummary/UpdateAttendanceSummary";
            return $http
                .post(url, angular.toJson(request));
        },
        saveAttendances: function(request) {
            var url = MasVidaApiUrl + "/Attendance/UpdateAttendances";
            return $http
                .post(url, angular.toJson(request));
        }
    };
});
